package com.haha.demo.filter;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 为什么不使用拦截器  拦截器是springMVC，需要触发*.do ，没有办法拦截静态的资源，所以使用过滤器
 */
@Component
public class UserInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //验证当前的session 是否存在用户
        String url = request.getRequestURI();
        if(url.contains(".css") || url.contains("js") || url.contains("login.html")
                || url.contains("login.do") || url.contains("createcode.do")){
            //放行
            return true;
        }else {
            if (request.getSession().getAttribute("user") == null) {
                //1、未登录
//            拦截

                //页面跳转
                response.sendRedirect("/login.html");
                return false;
            } else {
                //放行
                return true;
            }
        }
    }
}
